#include "lend.h"
#include "ui_lend.h"

Lend::Lend(User_t user, int flag, QWidget *parent) :
    QWidget(parent),user(user),flag(flag),
    ui(new Ui::Lend)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    ui->add_pushButton->hide();
    if(flag == 0)
    {
        QTableWidgetItem *headerItem;
        QStringList headerText;
        headerText<<"姓 名"<<"性 别"<<"出生日期"<<"民 族"; //表头标题用QStringList来表示
        //ui->tableInfo->setHorizontalHeaderLabels(headerText);
        ui->tableWidget->setColumnCount(headerText.count());//列数设置为与 headerText的行数相等
        for (int i=0;i<ui->tableWidget->columnCount();i++)//列编号从0开始
        {
            //cellItem=ui->tableInfo->horizontalHeaderItem(i);
            headerItem=new QTableWidgetItem(headerText.at(i)); //新建一个QTableWidgetItem， headerText.at(i)获取headerText的i行字符串
            QFont font=headerItem->font();//获取原有字体设置
            font.setBold(true);//设置为粗体
            font.setPointSize(12);//字体大小
            headerItem->setTextColor(Qt::black);//字体颜色
            headerItem->setFont(font);//设置字体
            ui->tableWidget->setHorizontalHeaderItem(i,headerItem); //设置表头单元格的Item
        }


        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget->verticalHeader()->setVisible(false);//隐藏表头
        ui->tableWidget->horizontalHeader()->setVisible(false);
        connect(ui->tableWidget,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(slotGetTableItem(QTableWidgetItem*)));

        ui->tableWidget->clearContents();//只清除工作区，不清除表头

        QTableWidgetItem *item;
        int row = 0;
        QList<Book_t> list = Sqlite::Get()->Select_book();
        foreach(Book_t book, list)
        {
            qDebug() << book.name << " " <<  book.num << " " <<  book.sell;
            int col = 0;
            item = new QTableWidgetItem();
            item->setText(book.name);
            item->setBackgroundColor(QColor("#8EE5EE"));//背景颜色
            item->setTextColor(QColor("#000"));//字体颜色
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            ui->tableWidget->insertRow(row);
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            item->setText("详情");
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            item->setText("借阅");
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            item->setText("预约");
            ui->tableWidget->setItem(row, col, item);

            row++;

        }
    }
    else if(flag == 1)
    {
        QTableWidgetItem *headerItem;
        QStringList headerText;
        headerText<<"姓 名"<<"性 别"; //表头标题用QStringList来表示
        //ui->tableInfo->setHorizontalHeaderLabels(headerText);
        ui->tableWidget->setColumnCount(headerText.count());//列数设置为与 headerText的行数相等
        for (int i=0;i<ui->tableWidget->columnCount();i++)//列编号从0开始
        {
            //cellItem=ui->tableInfo->horizontalHeaderItem(i);
            headerItem=new QTableWidgetItem(headerText.at(i)); //新建一个QTableWidgetItem， headerText.at(i)获取headerText的i行字符串
            QFont font=headerItem->font();//获取原有字体设置
            font.setBold(true);//设置为粗体
            font.setPointSize(12);//字体大小
            headerItem->setTextColor(Qt::black);//字体颜色
            headerItem->setFont(font);//设置字体
            ui->tableWidget->setHorizontalHeaderItem(i,headerItem); //设置表头单元格的Item
        }


        //ui->tableWidget->setShowGrid(true);
        //ui->tableWidget->setGridStyle(Qt::SolidLine);
        //ui->tableWidget->setModel(model);
        //ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);  //select a line
        //ui->tableWidget->horizontalHeader()->setHighlightSections(false);
        //ui->tableWidget->verticalHeader()->setVisible(false);
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        //ui->tableWidget->setStyleSheet("QHeaderView::section { background-color:green; color:white};");
        ui->tableWidget->verticalHeader()->setVisible(false);//隐藏表头
        ui->tableWidget->horizontalHeader()->setVisible(false);
        //ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        //ui->tableWidget->verticalHeader()->setMinimumWidth(42);
        //ui->tableWidget->horizontalHeader()->setMinimumHeight(30);
        connect(ui->tableWidget,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(slotGetTableItem(QTableWidgetItem*)));

        //ui->tableWidget->setColumnCount(4);
        ui->tableWidget->clearContents();//只清除工作区，不清除表头

        QTableWidgetItem *item;
        int row = 0;
        QList<Lend_t> list = Sqlite::Get()->Select_lend(user.id);
        foreach(Lend_t book, list)
        {
            //qDebug() << book.name << " " <<  book.num << " " <<  book.sell;
            int col = 0;
            item = new QTableWidgetItem();
            item->setText(book.book);
            item->setBackgroundColor(QColor("#8EE5EE"));//背景颜色
            item->setTextColor(QColor("#000"));//字体颜色
            item->setData(Qt::UserRole, book.book);
            item->setData(Qt::UserRole+1, book.sell);
            item->setData(Qt::UserRole+2, book.lend);
            ui->tableWidget->insertRow(row);
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.book);
            item->setData(Qt::UserRole+1, book.sell);
            item->setData(Qt::UserRole+2, book.lend);
            item->setText("归还");
            ui->tableWidget->setItem(row, col, item);

            row++;
        }
    }
    else if(flag == 2 || flag == 4)
    {
        ui->add_pushButton->show();

        QTableWidgetItem *headerItem;
        QStringList headerText;
        headerText<<"姓 名"<<"性 别" << "123"; //表头标题用QStringList来表示
        //ui->tableInfo->setHorizontalHeaderLabels(headerText);
        ui->tableWidget->setColumnCount(headerText.count());//列数设置为与 headerText的行数相等
        for (int i=0;i<ui->tableWidget->columnCount();i++)//列编号从0开始
        {
            //cellItem=ui->tableInfo->horizontalHeaderItem(i);
            headerItem=new QTableWidgetItem(headerText.at(i)); //新建一个QTableWidgetItem， headerText.at(i)获取headerText的i行字符串
            QFont font=headerItem->font();//获取原有字体设置
            font.setBold(true);//设置为粗体
            font.setPointSize(12);//字体大小
            headerItem->setTextColor(Qt::black);//字体颜色
            headerItem->setFont(font);//设置字体
            ui->tableWidget->setHorizontalHeaderItem(i,headerItem); //设置表头单元格的Item
        }


        //ui->tableWidget->setShowGrid(true);
        //ui->tableWidget->setGridStyle(Qt::SolidLine);
        //ui->tableWidget->setModel(model);
        //ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);  //select a line
        //ui->tableWidget->horizontalHeader()->setHighlightSections(false);
        //ui->tableWidget->verticalHeader()->setVisible(false);
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        //ui->tableWidget->setStyleSheet("QHeaderView::section { background-color:green; color:white};");
        ui->tableWidget->verticalHeader()->setVisible(false);//隐藏表头
        ui->tableWidget->horizontalHeader()->setVisible(false);
        //ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        //ui->tableWidget->verticalHeader()->setMinimumWidth(42);
        //ui->tableWidget->horizontalHeader()->setMinimumHeight(30);
        connect(ui->tableWidget,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(slotGetTableItem(QTableWidgetItem*)));

        //ui->tableWidget->setColumnCount(4);
        ui->tableWidget->clearContents();//只清除工作区，不清除表头

        QTableWidgetItem *item;
        int row = 0;
        QList<Book_t> list = Sqlite::Get()->Select_book();
        foreach(Book_t book, list)
        {
            qDebug() << book.name << " " <<  book.num << " " <<  book.sell;
            int col = 0;
            item = new QTableWidgetItem();
            item->setText(book.name);
            item->setBackgroundColor(QColor("#8EE5EE"));//背景颜色
            item->setTextColor(QColor("#000"));//字体颜色
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            ui->tableWidget->insertRow(row);
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            item->setText("详情");
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.name);
            item->setData(Qt::UserRole+1, book.num);
            item->setData(Qt::UserRole+2, book.sell);
            item->setData(Qt::UserRole+3, book.img);
            item->setText("下架");
            ui->tableWidget->setItem(row, col, item);

            row++;

        }
    }
    else if(flag == 3)
    {
        QTableWidgetItem *headerItem;
        QStringList headerText;
        headerText<<"用户名"<<"昵称" << "电话" << "借阅记录" << "短信通知"; //表头标题用QStringList来表示
        //ui->tableInfo->setHorizontalHeaderLabels(headerText);
        ui->tableWidget->setColumnCount(headerText.count());//列数设置为与 headerText的行数相等
        for (int i=0;i<ui->tableWidget->columnCount();i++)//列编号从0开始
        {
            //cellItem=ui->tableInfo->horizontalHeaderItem(i);
            headerItem=new QTableWidgetItem(headerText.at(i)); //新建一个QTableWidgetItem， headerText.at(i)获取headerText的i行字符串
            QFont font=headerItem->font();//获取原有字体设置
            font.setBold(true);//设置为粗体
            font.setPointSize(12);//字体大小
            headerItem->setTextColor(Qt::black);//字体颜色
            headerItem->setFont(font);//设置字体
            ui->tableWidget->setHorizontalHeaderItem(i,headerItem); //设置表头单元格的Item
        }


        //ui->tableWidget->setShowGrid(true);
        //ui->tableWidget->setGridStyle(Qt::SolidLine);
        //ui->tableWidget->setModel(model);
        //ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);  //select a line
        //ui->tableWidget->horizontalHeader()->setHighlightSections(false);
        //ui->tableWidget->verticalHeader()->setVisible(false);
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        //ui->tableWidget->setStyleSheet("QHeaderView::section { background-color:green; color:white};");
        ui->tableWidget->verticalHeader()->setVisible(false);//隐藏表头
        ui->tableWidget->horizontalHeader()->setVisible(true);
        //ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        //ui->tableWidget->verticalHeader()->setMinimumWidth(42);
        //ui->tableWidget->horizontalHeader()->setMinimumHeight(30);
        connect(ui->tableWidget,SIGNAL(itemClicked(QTableWidgetItem*)),this,SLOT(itemClick(QTableWidgetItem*)));

        //ui->tableWidget->setColumnCount(4);
        ui->tableWidget->clearContents();//只清除工作区，不清除表头

        QTableWidgetItem *item;
        int row = 0;
        QList<Lend_t> list = Sqlite::Get()->Select_lend();
        foreach(Lend_t book, list)
        {
            //qDebug() << book.name << " " <<  book.num << " " <<  book.sell;
            int col = 0;
            item = new QTableWidgetItem();
            item->setText(book.user);
            item->setBackgroundColor(QColor("#8EE5EE"));//背景颜色
            item->setTextColor(QColor("#000"));//字体颜色
            item->setData(Qt::UserRole, book.user);
            item->setData(Qt::UserRole+1, book.id);
            item->setData(Qt::UserRole+2, book.phone);
            item->setData(Qt::UserRole+3, book.book);
            item->setData(Qt::UserRole+4, book.sell);
            item->setData(Qt::UserRole+5, book.lend);
            item->setData(Qt::UserRole+6, book.img);
            ui->tableWidget->insertRow(row);
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.user);
            item->setData(Qt::UserRole+1, book.id);
            item->setData(Qt::UserRole+2, book.phone);
            item->setData(Qt::UserRole+3, book.book);
            item->setData(Qt::UserRole+4, book.sell);
            item->setData(Qt::UserRole+5, book.lend);
            item->setData(Qt::UserRole+6, book.img);
            item->setText( book.id);
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.user);
            item->setData(Qt::UserRole+1, book.id);
            item->setData(Qt::UserRole+2, book.phone);
            item->setData(Qt::UserRole+3, book.book);
            item->setData(Qt::UserRole+4, book.sell);
            item->setData(Qt::UserRole+5, book.lend);
            item->setData(Qt::UserRole+6, book.img);
            item->setText(book.phone);
            ui->tableWidget->setItem(row, col, item);

            col++;
            item = new QTableWidgetItem();
            item->setData(Qt::UserRole, book.user);
            item->setData(Qt::UserRole+1, book.id);
            item->setData(Qt::UserRole+2, book.phone);
            item->setData(Qt::UserRole+3, book.book);
            item->setData(Qt::UserRole+4, book.sell);
            item->setData(Qt::UserRole+5, book.lend);
            item->setData(Qt::UserRole+6, book.img);
            item->setText("详情");
            ui->tableWidget->setItem(row, col, item);

            col++;
            //item = new QTableWidgetItem();
            //item->setData(Qt::UserRole, book.user);
            //item->setData(Qt::UserRole+1, book.id);
            //item->setData(Qt::UserRole+2, book.phone);
            QVariant q;
            q.setValue(book);

            QComboBox *box = new QComboBox();
            box->addItem("下拉获取更多");
            box->addItem("通知还书");
            box->addItem("预约通知");
            box->setProperty("user", q);//book.user
            //box->setProperty("phone", book.phone);
            //box->setProperty("book", book.book);
            connect(box, SIGNAL(currentIndexChanged(QString)), this, SLOT(indexChange(QString)));

            //item->setText("短信通知");
            ui->tableWidget->setCellWidget(row, col, box);
            //ui->tableWidget->setItem(row, col, box);

            row++;
        }
    }

}

Lend::~Lend()
{
    delete ui;
}

//槽函数里面我们可以通过item获取很多单元格里的信息，比如item所在的行和列，文字等
void Lend::slotGetTableItem(QTableWidgetItem *item)
{
    Book_t b;
    if(flag == 0 || flag == 2 ||flag == 4)
    {
        b.name = item->data(Qt::UserRole).toString();
        b.num = item->data(Qt::UserRole+1).toInt();
        b.sell = item->data(Qt::UserRole+2).toInt();
        b.img = item->data(Qt::UserRole+3).toString();
        qDebug() << item->column() << item->text() << item->data(Qt::UserRole).toString() << " " << b.num << " " << b.sell;
    }
    if(item->text() == "详情")
    {
        if(flag != 4)
        {
            MsgDialog *m = new MsgDialog(b.img, b.name, b.num, b.sell);
            m->show();
        }
        else
        {
            BookMsg *m = new BookMsg(b.name);
            m->show();
        }
    }
    else if(item->text() == "借阅")
    {
        if(b.num == 0)
        {
            TipWidget *w = new TipWidget(b.name, 0);
            w->show();
        }
        else
        {
            if(user.money >= b.sell)
            {
                LendConform *l = new LendConform(b.name, b.sell);
                connect(l, &LendConform::confirm, this, [&, b]
                {
                    user.money -= b.sell;
                    int tmp = b.num-1;

                    Sqlite::Get()->Update_userMoney(user);
                    Sqlite::Get()->Update_bookNum(b.name, tmp);
                    Sqlite::Get()->Insert_lend(user, b.name, b.sell);

                    TipWidget *t = new TipWidget(b.name, 1);
                    t->show();
                    emit deleteMoney(user.money);
                });
                l->show();
            }
            else
            {
                TipWidget *w = new TipWidget(b.name, 4);
                w->show();
            }
        }

    }
    else if(item->text() == "预约")
    {
        TipWidget *w = new TipWidget(b.name, 2);
        w->show();

        Sqlite::Get()->Update_userOrder(user.id, b.name);
    }
    else if(item->text() == "归还")
    {
        TipWidget *w = new TipWidget(b.name, 3);
        w->show();

        welog_debug() << "归还";
        user.money += item->data(Qt::UserRole+1).toInt() + 2;
        QString lend = item->data(Qt::UserRole+2).toString();
        Sqlite::Get()->Update_userMoney(user);
        Sqlite::Get()->Delete_lend(user.id, item->data(Qt::UserRole).toString(), lend);
        Sqlite::Get()->Update_bookNum(item->data(Qt::UserRole).toString());

        int rowIdx =item->row();
        ui->tableWidget->removeRow(rowIdx);
        emit sigMoney(user.money);
    }
    else if(item->text() == "下架")
    {
        welog_debug() << b.name;
        Sqlite::Get()->Delete_book(b.name);
        TipWidget *w = new TipWidget(b.name, 5);
        w->show();
    }
}

void Lend::itemClick(QTableWidgetItem *item)
{
    welog_debug() << item->data(Qt::UserRole).toString();
    //b.name = item->data(Qt::UserRole).toString();
    //b.num = item->data(Qt::UserRole+1).toInt();
    //b.sell = item->data(Qt::UserRole+2).toInt();
    //qDebug() << item->column() << item->text() << item->data(Qt::UserRole).toString() << " " << b.num << " " << b.sell;

    if(item->text() == "详情")
    {
        Lend_t lend;
        lend.user = item->data(Qt::UserRole).toString();
        lend.id = item->data(Qt::UserRole+1).toString();
        lend.phone = item->data(Qt::UserRole+2).toString();
        lend.book = item->data(Qt::UserRole+3).toString();
        //lend.sell = item->data(Qt::UserRole+4).toString();
        lend.lend = item->data(Qt::UserRole+5).toString();

        LendMsg *m = new LendMsg(lend);
        m->show();
    }
}

void Lend::on_pushButton_clicked()
{
    this->close();
}

void Lend::indexChange(QString data)
{
    qDebug() << data;
    QComboBox *box = qobject_cast<QComboBox *>(sender());
    if(box)
    {
        QVariant v = box->property("user");
        Lend_t temp = v.value<Lend_t>();
        welog_debug() << temp.book;
#ifdef SUPPORT_ARM
        if(data == "通知还书")
        {
            User_t user;
            user.name = temp.user;
            user.phone = temp.phone;
            if(!temp.book.isEmpty())
            {
                SIM900A::Get()->setUser(user.phone, LEND_MSG);
                QMessageBox::about(this, "tips", "已发短信通知");
            }
            else
            {
                QMessageBox::about(this, "tips", "该读者暂没有借阅图书！");
            }
        }
        else if(data == "预约通知")
        {
            User_t user = Sqlite::Get()->Select_user(temp.id);
            if(!user.option.isEmpty())
            {
                SIM900A::Get()->setUser(temp.phone, ORDER_MSG);
                QMessageBox::about(this, "tips", "已发短信通知");
            }
            else
            {
                QMessageBox::about(this, "tips", "该读者暂没有预约图书！");
            }
        }

#endif
    }
    else
        welog_debug() << "fail";
}

void Lend::on_add_pushButton_clicked()
{
    welog_debug() << "on_add_pushButton_clicked";
    AddBook *a = new AddBook;
    connect(a, &AddBook::sigAddBook, this, [&]
    {
        updateBook();
    });
    a->show();
}

void Lend::updateBook()
{
    ui->tableWidget->clearContents();//只清除工作区，不清除表头

    QTableWidgetItem *item;
    int row = 0;
    QList<Book_t> list = Sqlite::Get()->Select_book();
    foreach(Book_t book, list)
    {
        qDebug() << book.name << " " <<  book.num << " " <<  book.sell;
        int col = 0;
        item = new QTableWidgetItem();
        item->setText(book.name);
        item->setBackgroundColor(QColor("#8EE5EE"));//背景颜色
        item->setTextColor(QColor("#000"));//字体颜色
        item->setData(Qt::UserRole, book.name);
        item->setData(Qt::UserRole+1, book.num);
        item->setData(Qt::UserRole+2, book.sell);
        item->setData(Qt::UserRole+3, book.img);
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, col, item);

        col++;
        item = new QTableWidgetItem();
        item->setData(Qt::UserRole, book.name);
        item->setData(Qt::UserRole+1, book.num);
        item->setData(Qt::UserRole+2, book.sell);
        item->setData(Qt::UserRole+3, book.img);
        item->setText("详情");
        ui->tableWidget->setItem(row, col, item);

        col++;
        item = new QTableWidgetItem();
        item->setData(Qt::UserRole, book.name);
        item->setData(Qt::UserRole+1, book.num);
        item->setData(Qt::UserRole+2, book.sell);
        item->setData(Qt::UserRole+3, book.img);
        item->setText("下架");
        ui->tableWidget->setItem(row, col, item);

        row++;

    }
}

